//
//  DatePickerView.swift
//  BJ12366Swift
//
//  Created by ZRAR on 16/3/16.
//  Copyright © 2016年 ZRAR. All rights reserved.
//

import UIKit


protocol SendValueDetegate{
    
    func valueChanged(newString:String)
}

typealias sendValueClosure = (string:String)->Void

class DatePickerView: UIView {
    
    @IBOutlet weak var datePicker: UIDatePicker!
    
    @IBOutlet weak var timeLabel: UILabel!
    
    var comfirClosure : sendValueClosure?
    var formatter:NSDateFormatter!
    var delegate : SendValueDetegate?
    static func ReadDatePickerView()->DatePickerView{
        
        let view = NSBundle.mainBundle().loadNibNamed("DatePickerView", owner: nil, options: nil).first as! DatePickerView
        
        view.frame.size = CGSizeMake(kUIScreenWidth, kUIScreenHeight)
        
        return view
    }
    
    
    func setDatePicker(formatter: NSDateFormatter, nowDate:NSDate, pickerMode:UIDatePickerMode){
        self.formatter = formatter
        datePicker.date = nowDate
        datePicker.datePickerMode = pickerMode
        timeLabel.text = formatter.stringFromDate(nowDate)
    }
    
    
    @IBAction func pickerViewValueChanged(sender: AnyObject) {
        
        
        let date = datePicker.date
        timeLabel.text = formatter.stringFromDate(date)
        
    }
    
    func showInView(){
        
        UIApplication.sharedApplication().keyWindow?.addSubview(self)
        
    }
    
    func comfirm(closures: sendValueClosure?){
        
        comfirClosure  = closures
        
    }
   
//    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
//        super.init(coder: aDecoder)
//    }

    @IBAction func hidView(sender: AnyObject) {
        
        self.removeFromSuperview()
    }
    
    @IBAction func cancelBtnAction(sender: AnyObject) {
        
        self.removeFromSuperview()
    }
    
    @IBAction func comfirBtnAction(sender: AnyObject) {
        //闭包
//        if let closure = comfirClosure {
//                closure(string:timeLabel.text!)
//        }
        //代理方法
        self.delegate?.valueChanged(timeLabel.text!)

        self.removeFromSuperview()
    }
}
